<template>
  <Dialog
    :title="props.editId ? '修改' : '新增'"
    @handle-confirm="handleConfirm"
    @closed="handleDialogClosed"
    @open="handleDialogOpen"
  >
    <el-form
      ref="formEl"
      :model="claimTypeForm"
      :rules="props.editId ? editRules : addRules"
      label-suffix=":"
      label-width="100px"
      label-position="right"
      v-loading="formLoading"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="claimTypeForm.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="" prop="required">
        <el-checkbox v-model="claimTypeForm.required"> 必要 </el-checkbox>
      </el-form-item>
      <!-- <el-form-item
        label=""
        prop="isStatic"
      >
        <el-checkbox
          v-model="claimTypeForm.isStatic"
        >
          静态
        </el-checkbox>
      </el-form-item> -->
      <el-form-item label="正则" prop="regex">
        <el-input v-model="claimTypeForm.regex" placeholder="请输入正则" />
      </el-form-item>
      <el-form-item label="正则描述" prop="regex">
        <el-input
          v-model="claimTypeForm.regexDescription"
          placeholder="请输入正则描述"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="claimTypeForm.description"
          placeholder="请输入描述"
        />
      </el-form-item>
      <el-form-item label="值类型" prop="valueType">
        <el-select v-model="claimTypeForm.valueType" placeholder="值类型">
          <el-option
            v-for="item in valueTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'

import Dialog from '@/components/Dialog/index.vue'
import { addClaimType, getClaimType, updateClaimType } from '@/http/claimType'
import { ClaimTypeValueTypeEnum, IAddClaimType } from '@/http/types/claimTypes'
import type { IElForm } from '@/types/element-plus'

const props = defineProps({
  editId: {
    type: String,
    default: '',
  },
})
interface EmitsType {
  (e: 'update:editId', value: string): void
  (e: 'success'): void
}
const emit = defineEmits<EmitsType>()

const valueTypes = [
  {
    label: 'String',
    value: ClaimTypeValueTypeEnum.String,
  },
  {
    label: 'Int',
    value: ClaimTypeValueTypeEnum.Int,
  },
  {
    label: 'Boolean',
    value: ClaimTypeValueTypeEnum.Boolean,
  },
  {
    label: 'DateTime',
    value: ClaimTypeValueTypeEnum.DateTime,
  },
]

const data = {
  name: '',
  required: false,
  isStatic: false,
  regex: '',
  regexDescription: '',
  description: '',
  valueType: 0,
  extraProperties: {},
}
const claimTypeForm = ref<IAddClaimType>(data)

const addRules = reactive({
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
})

const editRules = reactive({
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
})

const formEl = ref<IElForm>()

const handleConfirm = async () => {
  try {
    await formEl.value?.validate()
    if (props.editId) {
      await updateClaimType(
        props.editId,
        Object.assign(claimTypeForm.value, {
          concurrencyStamp: concurrencyStamp.value,
        }),
      )
    } else {
      await addClaimType(claimTypeForm.value)
    }
    emit('success')
    ElMessage.success('保存成功')
  } catch (err) {
    console.error(err)
  }
}

const formLoading = ref(false)
const concurrencyStamp = ref('')

const handleDialogOpen = async () => {
  formLoading.value = true
  const res = await getClaimType(props.editId).finally(() => {
    formLoading.value = false
  })
  claimTypeForm.value = {
    name: res.name,
    required: res.required,
    isStatic: res.isStatic,
    regex: res.regex,
    regexDescription: res.regexDescription,
    description: res.description,
    valueType: res.valueType,
    extraProperties: res.extraProperties,
  }
  concurrencyStamp.value = res.concurrencyStamp
}
const handleDialogClosed = () => {
  emit('update:editId', '')
  formEl.value?.resetFields() // 清除表单数据
  concurrencyStamp.value = ''
}
</script>

<style scoped></style>
